#!/usr/bin/env python

import tkFont

import os
import sys

import turandot
from turandot import config
from turandot.theme import *

theme = dict()

def no_action():
    """
    Stand-in for buttons that do not yet have an associated action. Prints an
    alert to standard error, but otherwise does nothing of consequence.
    """
    sys.stderr.write("button (or similar) has no associated action\n")

class AboutDialog(Toplevel):
    """ An about box for turandot, describing both the GUI and the toolchain.
    """ 
    
    def __init__(self, parent):
        Toplevel.__init__(self, parent)
        self.title("About Turandot")
        self.grab_set() # become modal
        self.resizable(0, 0)

        text = Label(self, font=theme["bold_font"], text="Turandot v%s" % config.VERSION)
        text.pack(pady=5, side=TOP)

        msg = Message(self, text="GUI for the Turandot toolchain.\n\nCopyright (c) 2011 The Turandot Development Group - see LICENSE for details", width=280)
        msg.pack(side=TOP)

        b = Button(self, text="Close", command=self.destroy)
        b.pack(pady=5, side=TOP)

class TurandotToolbar(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        
        self.add_button("New")
        self.add_button("Build")

        self.pack(anchor='n', side=TOP, fill=X)
   
    def add_button(self, label, action=no_action):
        """
        Add a standard button to the toolbar.
        """
        button = Button(self, text=label, command=action)
        button.pack(side=LEFT)

class TurandotCanvas(Canvas):
    """
    A graphical editor for the Turandot language.
    """
    def __init__(self, parent):
        Canvas.__init__(self, parent, borderwidth=2, relief='sunken')
        self.parent = parent

        self.pack(side=TOP, fill=BOTH, expand=True)

class TurandotEditor(Frame):
    """
    A text editor with features (such as highlighting and completion) built for
    the turandot syntax.
    """
    pass

class AssemblerEditor(Frame):
    """
    A text editor with features (such as highlighting and completion) built for
    standard assembly-like syntaxes. May need to be extended for the more
    complex assembly notations, otherwise highlighting and indentation will be
    funky (and possibly incorrect, depending on the exact demands of the
    syntax).
    """
    pass

class TurandotFrame(Frame):
    """
    The main display for the Turandot GUI.
    """
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent

        self.parent.title("Turandot")

        self.menu = Menu(self)
        self.file_menu = Menu(self.menu)
        self.menu.add_cascade(label="File", menu=self.file_menu)
        self.file_new_menu = Menu(self.file_menu)
        self.file_menu.add_cascade(label="New", menu=self.file_new_menu)
        self.file_menu.add_separator()
        self.file_menu.add_command(label="Close", command=self.parent.destroy)
        self.edit_menu = Menu(self.menu)
        self.menu.add_cascade(label="Edit", menu=self.edit_menu)
        self.help_menu = Menu(self.menu)
        self.menu.add_cascade(label="Help", menu=self.help_menu)
        self.help_menu.add_command(label="About", command=self.show_about)

        self.parent.config(menu=self.menu);

        self.toolbar = TurandotToolbar(self)
        self.canvas = TurandotCanvas(self)

        self.pack(fill=BOTH, expand=True, side=TOP)

    def show_about(self):
        about_dialog = AboutDialog(self)
        self.parent.wait_window(about_dialog)

def main():
    root = Tk()
   
    theme["bold_font"] = tkFont.Font(weight="bold")

    app = TurandotFrame(root)
    root.mainloop()

if __name__ == '__main__':
    main()
